import { createSlice } from "@reduxjs/toolkit"
import { getAppointment } from "../actions/appointmentActions"
import { action } from "mobx"
import { Appointments } from "app/screens"

interface AppointmentState {
  loading?: boolean
  Appointments?: object[]
  error?: string
  offset?: number
  count?: number
  limit?: number
  mode?: string
}
const initialState: AppointmentState = {
  loading: false,
  Appointments: [],
  error: "",
  offset: 0,
  count: 0,
  limit: 10,
  mode: "list",
}

const AppointmentSlice = createSlice({
  name: "appointment",
  initialState,
  reducers: {
    increaseOffset: (state) => {
      state.offset += state.limit
    },
    setAppointmentLoader: (state, action) => {
      state.loading = action?.payload
    },
    resetAppointment: (state) => {
      state.Appointments = []
      state.count = 0
      state.offset = 0
    },
    setAppointmentMode: (state, action) => {
      state.mode = action?.payload
    },
  },
  extraReducers: (builder) => {
    //Reducer to get access Locks
    builder
      .addCase(getAppointment.pending, (state, { payload }) => {
        state.loading = true
      })
      .addCase(getAppointment.fulfilled, (state, { payload }) => {
        state.loading = false
        state.error = ""
        state.Appointments = [...state.Appointments, ...payload?.result]
        state.offset = payload?.offset
        state.count = payload?.count
        console.log("getAppointment successfull")
      })
      .addCase(getAppointment.rejected, (state, { payload }) => {
        state.loading = false
        state.error = ""
        console.log("rejected in getAppointment", payload)
      })
  },
})

export const { increaseOffset, setAppointmentLoader, resetAppointment, setAppointmentMode } =
  AppointmentSlice.actions

export default AppointmentSlice.reducer
